package com.hd123.data.check.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Role;

import javax.annotation.PreDestroy;
import javax.sql.DataSource;

import java.sql.SQLException;
import java.util.Properties;

import static org.springframework.beans.factory.config.BeanDefinition.ROLE_INFRASTRUCTURE;

/**
 * description:
 * druid连接池
 *
 * Date: 2017/12/21
 * Time: 17:05
 *
 * @author zhangxin
 */
@Configuration
public class DruidDataSourceConfigration {
    private static final Logger logger = LoggerFactory.getLogger(DruidDataSourceConfigration.class);

    /**
     * druid连接池
     */
    private volatile DruidDataSource pool;

    @Autowired
    private DruidDataSourceProperties config;

    @Primary
    @Autowired
    @Role(ROLE_INFRASTRUCTURE)
    @Description("替换默认数据源为阿里巴巴druid数据源")
    @Bean(name = "dataSource", destroyMethod = "close")
    public DataSource dataSource() {
        pool = new DruidDataSource();
        pool.setUrl(config.getUrl());
        pool.setDriverClassName(config.getDriverClass());
        pool.setUsername(config.getUsername());
        pool.setPassword(config.getPassword());
        pool.setInitialSize(config.getInitialSize());
        pool.setMaxActive(config.getMaxActive());
        pool.setMinIdle(config.getMinIdle());
        pool.setTestOnBorrow(config.isTestOnBorrow());
        pool.setTestOnReturn(config.isTestOnReturn());
        pool.setUseUnfairLock(config.isUseUnfairLock());
        pool.setPoolPreparedStatements(config.isPoolPreparedStatements());
        pool.setLogAbandoned(config.isLogAbandoned());
        pool.setRemoveAbandoned(config.isRemoveAbandoned());
        pool.setRemoveAbandonedTimeoutMillis(config.getTimeBetweenEvictionRunsMillis());
        pool.setTestWhileIdle(config.isTestWhileIdle());
        pool.setTimeBetweenEvictionRunsMillis(config.getTimeBetweenEvictionRunsMillis());
        pool.setMinEvictableIdleTimeMillis(config.getMinEvictableIdleTimeMillis());
        pool.setValidationQuery(config.getValidationQuery());
        pool.setValidationQueryTimeout(config.getValidationQueryTimeout());
        pool.setMaxPoolPreparedStatementPerConnectionSize(config
                .getMaxPoolPreparedStatementPerConnectionSize());

        // 监控
        try {
            Properties properties = new Properties();
            properties.put("druid.stat.slowSqlMillis", "5000");
            pool.setConnectProperties(properties);
            pool.setUseGlobalDataSourceStat(true);
            pool.setFilters("stat,mergeStat,counter,wall,commonLogging");
        } catch (SQLException e) {
            if (logger.isWarnEnabled()) {
                logger.warn("Druid SQLException Notice", e);
            }
        }

        return pool;
    }

    @PreDestroy
    public void close() {
        if (pool != null) {
            pool.close();
        }
    }


}
